package com.myc.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.myc.subject.application.convert.SubjectLikedDTOConverter;
import com.myc.subject.application.dto.SubjectLikedDTO;
import com.myc.subject.common.entity.Result;
import com.myc.subject.common.util.LoginUtil;
import com.myc.subject.domain.entity.SubjectLikedBO;
import com.myc.subject.domain.service.SubjectLikedDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *
 * 点赞Controller
 * */
@RestController
@RequestMapping("/subjectLiked/")
@Slf4j
public class SubjectLikedController {

    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(),"题目id不能为空");
            Preconditions.checkNotNull(subjectLikedDTO.getStatus(),"点赞状态不能为空");
            String loginID = LoginUtil.getLoginId();
            subjectLikedDTO.setLikeUserId(loginID);
            Preconditions.checkNotNull(subjectLikedDTO.getLikeUserId(),"点赞人不能为空");
            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);
            subjectLikedDomainService.add(subjectLikedBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectLikedController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }
}
